package com.aiwu.pop;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.aiwu.ArchiveBackupManger;
import com.aiwu.adapter.BakAdapter;
import com.aiwu.library.AiwuSimulator;
import com.aiwu.library.bean.ArchiveBean;
import com.aiwu.library.util.DialogUtil;

import org.citra.citra_emu.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 备份列表弹窗
 */
public class BakListPop extends AlertDialog implements View.OnClickListener, DialogInterface.OnShowListener, DialogInterface.OnDismissListener {
    private Context context;
    private ImageView btnClose;
    private ListView listView;
    private BakAdapter adapter;

    public BakListPop(Context context) {
        super(context, R.style.AiWuDialog);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
        initView();
    }

    private void init() {
        setContentView(R.layout.pop_back_up_list);
        setCancelable(true);
        setCanceledOnTouchOutside(true);
        setOnShowListener(this);
        setOnDismissListener(this);
    }

    private void initView() {
        btnClose = findViewById(R.id.btn_close);
        btnClose.setOnClickListener(this);
        listView = findViewById(R.id.listView);
        listView.setEmptyView(findViewById(R.id.emptyView));
        adapter = new BakAdapter();
        listView.setAdapter(adapter);
        adapter.setBtnClickListener(new BakAdapter.BtnClickListener() {
            @Override
            public void readArchiveBtnClick(int id, int position) {
                showReadTip(id);
            }

            @Override
            public void deleteArchiveBtnClick(int id, int position) {
                showDeleteTip(id, position);
            }
        });
    }

    private void refreshData() {
        List<ArchiveBean> list = ArchiveBackupManger.getInstance().getArchiveList();
        if (adapter != null)
            adapter.setData(list == null ? new ArrayList<>() : list);
    }

    @Override
    public void show() {
        super.show();
        DialogUtil.setDialog(this, 0.8f, 0.8f, 0.8f, 0.6f);
        refreshData();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_close) {
            dismiss();
        }
    }

    private void showReadTip(final int id) {
        DialogUtil.showTip(context, "是否读取备份", v -> {
            dismiss();
            ArchiveBackupManger.getInstance().readArchive(id);
        }, null);
    }

    private void showDeleteTip(final int id, final int position) {
        DialogUtil.showTip(context, "是否删除备份", v -> {
            adapter.removeData(position);
            ArchiveBackupManger.getInstance().deleteArchive(id);
        }, null);
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        //恢复游戏
        if (AiwuSimulator.getSimulatorCallback() != null) {
            AiwuSimulator.getSimulatorCallback().resume();
        }
    }

    @Override
    public void onShow(DialogInterface dialog) {
        //暂停游戏
        if (AiwuSimulator.getSimulatorCallback() != null) {
            AiwuSimulator.getSimulatorCallback().pause();
        }
    }
}
